package edu.uwlax.cs.oayonlinestore.client.gui;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.User;
import edu.uwlax.cs.oayonlinestore.client.User.UserChangeListener;
import edu.uwlax.cs.oayonlinestore.client.utils.SwitchPage;

public class Login extends SimplePanel implements UserChangeListener {

	private class LoginBox extends HorizontalPanel {
		
		private final static String STYLE_CREATE_ACC_LINK = "LoginCreateAcc";

		private Hyperlink createAccount = new Hyperlink(
				"Create<br>Account", true, "CreateAccount");

		private  KeyboardListener keyListener = new KeyboardListener() {
			public void onKeyDown(Widget arg0, char arg1, int arg2) {
				if(arg1 == KEY_ENTER)
					login.click();
			}

			public void onKeyPress(Widget arg0, char arg1, int arg2) {
			}

			public void onKeyUp(Widget arg0, char arg1, int arg2) {
			}};

		private Button login = new Button("Login");

		private TextBox name = new TextBox();
		
		private PasswordTextBox pwd = new PasswordTextBox();

		public LoginBox() {
			super();
			login.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					OnlinestoreRService.Util.getInstance().login(
							name.getText(), pwd.getText(), User.get());
				}
			});
			createAccount.addClickListener(new SwitchPage(Store.getFrame()
					.getCreateAcc().getAnchor()));
			createAccount.setStyleName(STYLE_CREATE_ACC_LINK);
			login.setStyleName(STYLE_BUTTON);
			name.setStyleName(STYLE_EDIT);
			pwd.setStyleName(STYLE_EDIT);
			name.addKeyboardListener(keyListener);
			pwd.addKeyboardListener(keyListener);
			add(name);
			add(pwd);
			add(login);
			add(createAccount);
		}
	}

	private class LogoutBox extends HorizontalPanel {

		private Button logout = new Button("Logout");

		private HTML name = new HTML();

		public LogoutBox() {
			super();
			logout.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					OnlinestoreRService.Util.getInstance().logout(User.get());
				}
			});
			logout.setStyleName(STYLE_BUTTON);
			add(name);
			add(logout);
		}
	}

	private final static String STYLE_BUTTON = "LoginBoxButton";

	private final static String STYLE_EDIT = "LoginBoxEdit";

	private final static String STYLE_LOGIN = "LoginBox";

	private LoginBox loginBox = new LoginBox();

	private LogoutBox logoutBox = new LogoutBox();

	public Login() {
		super();
		setWidget(loginBox);
		setStyleName(STYLE_LOGIN);
		User.get().addChangeListener(this);
	}

	public SimplePanel getFrame() {
		return this;
	}

	private void showLogin(User user) {
		Login.this.setWidget(loginBox);
	}

	private void showLogout(User user) {
		Login.this.setWidget(logoutBox);
		logoutBox.name.setHTML("<nobr><b>" + user.getFirstname() + " "
				+ user.getLastname() + "</b></nobr>&nbsp;&nbsp;");
	}

	public void userChanged(User user) {
		if (user.getId() == 0)
			showLogin(user);
		else
			showLogout(user);
	}
}
